/** 
* @Title: ThreadPoolTest.java 
* @Package com.lh.threadtest.t8 
* @Description: TODO
* @author Liu 
* @date 2018年1月17日 下午2:17:40 
* @version V1.0 
*/
package com.lh.threadtest.t8;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/** 
* @ClassName: ThreadPoolTest 
* @Description: java5线程并发库的应用(常见几种线程池类型)
* @author Liu
* @date 2018年1月17日 下午2:17:40 
*  
*/
public class ThreadPoolTest {

	/***
	* @Title: main 
	* @Description: TODO
	* @param @param args
	* @return void
	* @throws 
	*/
	public static void main(String[] args) {
		//创建固定大小的线程池
//		ExecutorService executorService = Executors.newFixedThreadPool(3);
		
		//创建缓存线程池
//		ExecutorService executorService = Executors.newCachedThreadPool();
		
		//创建单一线程池(如何实现线程死掉后重启？)
		ExecutorService executorService = Executors.newSingleThreadExecutor();
	
		
		for(int i = 1; i <= 10; i++){
			final int task = i;
			executorService.execute(new Runnable() {
				
				@Override
				public void run() {
					for(int i = 1; i <= 10; i++){
						try {
							//模拟处理延时
							TimeUnit.MILLISECONDS.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						System.out.println(Thread.currentThread().getName() + " is looping of: " + i + " for task of " + task);
					}
				}
			});
		}
	
		System.out.println("All of 10 tasks have commited!");
//		executorService.shutdownNow();
		executorService.shutdown();
	}

}
